package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.Greedy;

/**
 * 一次买卖
 * see 123 188
 * https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/tuan-mie-gu-piao-wen-ti
 *
 * @author tzp
 * @since 2020/9/18
 */
public class LC121 implements Greedy {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) return 0;
        int currentMin = prices[0], max = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < currentMin) {
                currentMin = prices[i];
            } else {
                max = Math.max(max, prices[i] - currentMin);
            }
        }
        return max;
    }
}
